﻿//---------------------------------------------------------------
// Copyright (C) 2007 Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
var map = null;
var coord = null;
var _Error = false;
function pageLoad() 
{
   if( map != null ) return;
   map = new VEMap("roadtripmap");
   map.LoadMap();
   map.ShowDisambiguationDialog(false);
   map.SetMapStyle(VEMapStyle.Road);
   map.HideDashboard();
   map.SetCenter(new VELatLong(47.22, -122.44));
   pageLoad1();
   map.AttachEvent("onclick", AddPushpinOnClick);
   
}
function AddPushpinOnClick(e)
{
    var panel = document.getElementById('pushpinpanel');
    if( e.rightMouseButton && panel.style.visibility != 'visible' )
    {
        panel.style.left = (document.body.offsetWidth/2) - 145;
        panel.style.top = (document.body.offsetHeight/2) - 95;
        coord = e;
        panel.style.visibility = 'visible';
    }
}

function onCreateRoute()
{
    var startloc = document.getElementById('StartLocation');
    var endloc = document.getElementById('EndLocation');
    var emailid = document.getElementById('EmailID');
    var title = document.getElementById('RouteTitle');
    var routelist = document.getElementById('RouteList');
    
    if( startloc.value == "" )
    {
        alert('Start Location is Empty');
        return;
    } 
    
    if( endloc.value == "" )
    {
        alert('End Location is Empty');
        return;
    } 
    
    if( emailid.value == "" )
    {
        alert('User Id not found');
        return;
    } 
    
    if( title.value == "" )
    {
        alert('Title not found');
        return;
    } 
    
    map.DeleteRoute();
    map.DeleteAllShapes();
    document.getElementById('RouteID').value = '';
    try
    {
        map.GetRoute(startloc.value, endloc.value, [VEDistanceUnit.Miles], [VERouteType.Shortest]);
    }
    catch(e)
    {
        alert(e.description);
        return;
    }
    var tool = document.getElementById('toolpanel');
    tool.style.visibility = "hidden";
    SetEditMode()
    Splash.show(title.value, '<p><span class=nowrap> Right click on the location where you want to Add Photos </span></p>',5);
}

function SetEditMode()
{
    document.getElementById('RouteNew').style.visibility = "hidden";
    document.getElementById('RouteSave').style.visibility = "visible";
    document.getElementById('RouteDelete').style.visibility = "visible";
    document.getElementById('RouteView').style.visibility = "visible";
    document.getElementById('RouteNew2').style.visibility = "visible";
    document.getElementById('RouteSave2').style.visibility = "hidden";
    document.getElementById('RouteDelete2').style.visibility = "hidden";
    document.getElementById('RouteView2').style.visibility = "hidden";
}

function SetCreateMode()
{
    ClearAll();
    document.getElementById('RouteNew').style.visibility = "visible";
    document.getElementById('RouteSave').style.visibility = "hidden";
    document.getElementById('RouteDelete').style.visibility = "hidden";
    document.getElementById('RouteView').style.visibility = "hidden";
    document.getElementById('RouteNew2').style.visibility = "hidden";
    document.getElementById('RouteSave2').style.visibility = "visible";
    document.getElementById('RouteDelete2').style.visibility = "visible";
    document.getElementById('RouteView2').style.visibility = "visible";
}

function onCreatePushPin()
{
    var title = document.getElementById('Title');
    var photourl = document.getElementById('PhotoURL');
    var infourl = document.getElementById('MoreInfoURL');
    var description = document.getElementById('Description').value;
    
    if( photourl.value == null || photourl.value == '' ) return;
    
    var pin = new VEShape(VEShapeType.Pushpin, map.PixelToLatLong(new VEPixel(coord.mapX, coord.mapY)));
    pin.SetCustomIcon("<span style='font-family:Arial; font-size:x-small; color:Black; background-color:White'><img src='images/iconphoto.png' style='width:20px; height:20px;' /></span>"); 
    pin.SetTitle('<H2 style = position:absolute;top:0px;height:50px;width:250px;left:0px;text-align:center;>' + title.value + '</H2>');
    pin.SetMoreInfoURL(infourl.value);
    pin.SetPhotoURL(photourl.value);
    map.AddShape(pin);
    description = '<table><tr><td> ' +  description + ' </td></tr><tr><td><div id=pushpinid onclick=onRemovePushPin(\''+ pin.GetID()+'\'); class=removepushpin> Remove Location </div> </td> </tr> </table>';
    pin.SetDescription(description);
    document.getElementById('pushpinpanel').style.visibility = 'hidden';
    document.getElementById('PhotoControl').style.visibility = "hidden";
}

function onRemovePushPin(e)
{
   map.DeleteShape(map.GetShapeByID(e));
}

function onCancelPushPin()
{
   document.getElementById('pushpinpanel').style.visibility = 'hidden';
   document.getElementById('PhotoControl').style.visibility = "hidden";

}

function ClearAll()
{
    map.DeleteRoute();
    map.DeleteAllShapes();
}

function GetCurrentRoute()
{
    var rl = document.getElementById('RouteList');
    var layer = map.GetShapeLayerByIndex(0);
    var Total = layer.GetShapeCount();
    var index = 0;
    var route = new RoadTripEx.Route();
    route.PushPinList = new Array();
    route.Routeidentifier = new RoadTripEx.RouteItinerary();
    route.Start = document.getElementById('StartLocation').value;
    route.End = document.getElementById('EndLocation').value  ;
    route.Routeidentifier.Name = document.getElementById('RouteTitle').value;
    route.Routeidentifier.Id = document.getElementById('RouteID').value;
  
    for( index = 0; index < Total; index++ )
    {
      var shape = layer.GetShapeByIndex(index);
      var pts = shape.GetPoints();
      var description = shape.GetDescription();
      var start = description.indexOf('<table>' ) + 16;
      var end =  description.indexOf('</td></tr><tr><td><div' ) -1;
      description = description.substring(start, end);
      var title = shape.GetTitle();
      var pin = new RoadTripEx.PushPin();
      start = title.indexOf('>' ) + 1;
      end =  title.indexOf('</' ) ;
      pin.Title = title.substring(start, end);
      pin.Latitude = pts[0].Latitude;
      pin.Longitude = pts[0].Longitude;
      pin.Photourl = shape.GetPhotoURL();
      pin.Moreinfourl = shape.GetMoreInfoURL();
      pin.Description = description;
      route.PushPinList[index] = pin;
    }
    return route;
}

function PopulateMap(result)
{
    ClearAll();
    if( result.Start.length >  1 || result.End.length > 1)
    {
        try
        {
             map.GetRoute(result.Start, result.End, [VEDistanceUnit.Miles], [VERouteType.Shortest]);
        }
        catch(e)
        {
            alert(e.description);
           
        }
        document.getElementById('StartLocation').value = result.Start;
        document.getElementById('EndLocation').value = result.End;
        document.getElementById('RouteTitle').value = result.Routeidentifier.Name;
        document.getElementById('RouteID').value = result.Routeidentifier.Id;
    }
    for(var item in result.PushPinList)
    {  
        var pin = new VEShape(VEShapeType.Pushpin,new VELatLong(+result.PushPinList[item].Latitude, +result.PushPinList[item].Longitude));
        pin.SetCustomIcon("<span style='font-family:Arial; font-size:x-small; color:Black; background-color:White'><img src='images/iconphoto.png' style='width:20px; height:20px;' /></span>"); 
        pin.SetTitle('<H2 style = position:absolute;top:0px;height:50px;left:0px;width:250px;text-align:center;>' + result.PushPinList[item].Title + '</H2>');
        pin.SetMoreInfoURL(result.PushPinList[item].Moreinfourl);
        pin.SetPhotoURL(result.PushPinList[item].Photourl);
        map.AddShape(pin);
        description = '<table><tr><td> ' + result.PushPinList[item].Description + '</td></tr><tr><td><div id=pushpinid onclick=onRemovePushPin(\''+ pin.GetID()+'\'); class=removepushpin> Remove Location </div> </td> </tr> </table>';
        pin.SetDescription(description);
    }
    var tool = document.getElementById('toolpanel');
    tool.style.visibility = "hidden";
    SetEditMode();
    Splash.show( result.Routeidentifier.Name, '<p><span class=nowrap> Right click on the location where you want to Add Photos  or click to remove photos</span></p>',5);
}

function pageUnload() 
{
    if (map!=null) 
    {
        map.Dispose();
        map = null;
    }
}

 function onSignin() 
    {		
        _Error = false;
    }
    function onSignout() 
    {		// user is not signed in 
    }function onError() 
    {		// there was a data transfer error
        _Error = true;
        //Splash.show("oops...", "Sorry. The Windows Live Contacts&trade; service is down. Please try again later." );
    }
    function onData(p_contacts) 
    {		// here comes the data!	
        var url, mailto, mtUrl, msg;
    	var s2= document.getElementById('RouteTitle').value ;
    	var s = '<h3> From : '+ document.getElementById('StartLocation').value + '</br> To : ' + document.getElementById('EndLocation').value +' </h3>  </br>';
    	s += 'Click each link below to email the personalized driving directions to your friends. </br> \n';
    	mailto = "mailto:{0}?subject={1}&body={2}";
    	url = "http://" + window.location.host + '/RoadTrip/RoadTripViewer.aspx?IID=' + document.getElementById('RouteID').value;

    	for (var i = 0; i < p_contacts.length; i++) 
    	{	
    	    	msg = p_contacts[i].name + ",\n\n I would like to share my pictures taken during the trip Click on the URL below for personalized driving directions  \n" + url;
				mtUrl = mailto.format(p_contacts[i].email, s2, msg );
				s += ('<div class="routeName"><b>{1}</b> <a href="{0}" title= Road Trip > Email </a> <a target="_blank" href="{2}" title= See what they will see> Preview</a></div>').format(mtUrl, p_contacts[i].name, url);
    	}	
    	Splash.show('<h1>'+ s2 + '</h1>', s, false,true ); 	
    }
    function onPhotoData(p_items) 
    {
		for (var i = 0; i < p_items.length; i++) 
		{
		    document.getElementById('PhotoURL').value = p_items[i].fileAccessControlledURL;
		    document.getElementById('PhotoControl').style.visibility = "hidden";
		    return;
		}
		
}

//window.attachEvent('onload', pageLoad);
window.attachEvent('onunload', pageUnload);